#include <vector>
#include <iostream>

using namespace std;

class Solution 
{
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target)
	{
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        size_t m = matrix.size();
		if(m<=0)
			return false;

		size_t n = matrix[0].size();
		if(n<=0)
			return false;

		size_t left = 1, right = m, center;

		while(left<=right)
		{
			center = (left+right)/2-1;
			if(matrix[center][0]==target)
				return true;
			else if(matrix[center][0] > target)
				right = center;
			else
				left = center+2;
		}

		if(right == 0)
			return false;
		size_t r = right-1;
		left = 1, right = n;
		while(left<=right)
		{
			center=(left+right)/2-1;
			if(matrix[r][center]==target)
				return true;
			else if(matrix[r][center] >target)
				right = center;
			else
				left = center+2;
		}

		return false;
    }
};


int main()
{
	int m,n;
	Solution s;
	while(cin>>m)
	{
		if(m==0)
			break;
		int k;
		cin>>n>>k;
		vector<vector<int> > matrix(m);
		for(int i=0;i<m;i++)
		{
			matrix[i] = vector<int>(n);
			for(int j=0;j<n;j++)
				cin>>matrix[i][j];
		}

		cout<<s.searchMatrix(matrix,k)<<endl;
	}
}